﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using ihongma.Web.Plugin.Advert;
using Ivony.Fluent;
using Ivony.Html;
using Ivony.Html.Parser;
using ihongma.Common;
using System.Xml;
using System.IO;

namespace ihongma.Job
{
   public class JobAd
    {
        JumonyParser parser = new JumonyParser();
        private Model.siteconfig siteConfig;
        public JobAd()
        {
            siteConfig = new BLL.siteconfig().loadConfig(Utils.GetXmlMapPath("Configpath"));
        }
        public void adadd()
        {
            //bool result = true;

            ihongma.Web.Plugin.Advert.Model.advert_banner model = new ihongma.Web.Plugin.Advert.Model.advert_banner();
            ihongma.Web.Plugin.Advert.BLL.advert_banner bll = new ihongma.Web.Plugin.Advert.BLL.advert_banner();
            BLL.article article = new BLL.article();
            var tab = article.GetList(7, "", "sort_id asc,add_time desc").Tables[0];
            foreach (DataRow item in tab.Rows)
            {
                if (!bll.Existstitle(item["title"].ToString()))
                {
                    string imgurl = img(item["content"].ToString());
                    if (imgurl != string.Empty)
                    {
                        model.aid = 1;
                        model.title = item["title"].ToString();
                        model.start_time = DateTime.Now;
                        model.end_time = DateTime.Now.AddMonths(2);
                        model.file_path = imgurl;
                        model.link_url = "http://www.ihongma.com/article/show/" + item["id"].ToString() + ".aspx";
                        model.content = item["zhaiyao"].ToString();
                        model.sort_id = 11;
                        model.is_lock = 0;
                        model.add_time = DateTime.Now;
                        bll.Add(model);
                    }
                }
            }

        }
        private string img(string content)
        {
            string img = string.Empty;
            string serverThumbnailFileName = string.Empty;
            var dataDocument = parser.Parse(content);
            try
            {
                img = dataDocument.Find("img").ElementAt(0).Attribute("src").Value();
            }
            catch (Exception)
            {

                return serverThumbnailFileName;
            }

            //string fileExt =img.Substring(img.LastIndexOf(".") + 1); //文件扩展名，不含“.”
            //    string fileName = Utils.GetRamCode() + "." + fileExt; //随机文件名
            //    string dirPath = GetUpLoadPath(); //上传目录相对路径

            Stream stream = Thumbnail.GetRemoteImage(img);
            if (stream == null)
                return serverThumbnailFileName;
            System.Drawing.Image original = System.Drawing.Image.FromStream(stream);

            stream.Close();
            if (original.Width >= 470 && original.Height >= 260)
            {

                serverThumbnailFileName = img;
            }
            return serverThumbnailFileName;
        }
        #region 私有方法

        /// <summary>
        /// 返回上传目录相对路径
        /// </summary>
        /// <param name="fileName">上传文件名</param>
        private string GetUpLoadPath()
        {
            string path = siteConfig.webpath + siteConfig.attachpath + "/"; //站点目录+上传目录
            switch (this.siteConfig.attachsave)
            {
                case 1: //按年月日每天一个文件夹
                    path += DateTime.Now.ToString("yyyyMMdd");
                    break;
                default: //按年月/日存入不同的文件夹
                    path += DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("dd");
                    break;
            }
            return path + "/";
        }
        #endregion
    }
}
